﻿using System;
using System.Collections.Generic; 
using NHunspell;

namespace KnowledgeSource
{
    public class FindingSynonyms //: IKnowledgeSource
    {

        private MyThes thes;
        private Hunspell hunspell;
        private Utils util;
        private int MAX_COUNT = 5;
        public FindingSynonyms(Hunspell hunspell, MyThes thes)
        {
            this.thes = thes;
            this.hunspell = hunspell;
            this.util = new Utils();
        }

        public List<string> execute( string input )
        {
            var sentence = util.MakeListStringFromSentence( input);
            var possibleResults = new List<List<string>>();
            int actualWord = 0;

            try
            {
                foreach (var word in sentence)
                {
                    var stem = hunspell.Stem(word)[0];
                    possibleResults.Add(new List<string>());
                    ThesResult tr = thes.Lookup(stem, hunspell);
                    if (tr != null && word.Length >= 3)
                    {
                        var synonyms = tr.Meanings[0].Synonyms;
                        var maxCount = MAX_COUNT > synonyms.Count ? synonyms.Count : MAX_COUNT;
                        if (tr.IsGenerated == true )
                        {
                            possibleResults[actualWord].AddRange(util.RemoveRedundantDescription(synonyms.GetRange(0, maxCount)));
                        }
                        else
                        {
                            possibleResults[actualWord].AddRange(util.RemoveRedundantDescription(synonyms.GetRange(0, maxCount)));
                        }
                    }
                    else
                    {
                        possibleResults[actualWord].Add(word);
                    }
                    actualWord++;
                }
                var temp = new List<string>();
                temp.Add(input);
                temp.AddRange(util.CreateSentenceFromAllPosibilities(possibleResults));
                return temp;
            }
            catch (Exception ex)
            {
                var t = new List<string>();
                t.Add(input);
                return t;
            }
             

        }
    }
}
